package datastructure;

import java.util.*;

import ICTCLAS.I3S.AC.ICTCLAS50;

public class Candidateprc {
	public String content;
	public String tag_content;
	public ArrayList<word> keyword;
	public ArrayList<word> words;
	public ArrayList<word> viword;
	public boolean combine;
	public Candidateprc(String a, boolean c)
	{
		content = a;
		words = new ArrayList<word>();
		keyword = new ArrayList<word>();
		viword = new ArrayList<word>();
		combine = c;
		parse();
	}
	
	public void addword(word a)
	{
		if(!words.contains(a))
		{
			words.add(a);
		}
	}
	
	public boolean containskeyword(word a)
	{
		for(int i = 0; i < keyword.size(); i++)
		{
			if(keyword.get(i).pos.equals(a.pos) && keyword.get(i).content.equals(a.content))
			{
				return true;
			}
		}
		return false;
	}
	
	public boolean containsviword(word a)
	{
		for(int i = 0; i < viword.size(); i++)
		{
			if(viword.get(i).pos.equals(a.pos) && viword.get(i).content.equals(a.content))
			{
				return true;
			}
		}
		return false;
	}
	
	public void parse()
	{
		try
		{
			ICTCLAS50 testICTCLAS50 = new ICTCLAS50();
			String argu = ".";
			//初始化
			if (testICTCLAS50.ICTCLAS_Init(argu.getBytes("UTF-8")) == false)
			{
				System.out.println("Init Fail!");
				return;
			}


			//设置词性标注集(0 计算所二级标注集，1 计算所一级标注集，2 北大二级标注集，3 北大一级标注集)
			testICTCLAS50.ICTCLAS_SetPOSmap(0);


			//导入用户词典前分词
			byte nativeBytes[] = testICTCLAS50.ICTCLAS_ParagraphProcess(content.getBytes("UTF-8"), 0, 1);//分词处理
			//System.out.println(nativeBytes.length);
			String nativeStr = new String(nativeBytes, 0, nativeBytes.length, "UTF-8");
			tag_content = nativeStr;
			//System.out.println("未导入用户词典的分词结果： " + nativeStr);//打印结果
			//处理结果转化
			
			extractword();
			//for(int i = 0; i < words.size(); i++)
			//{
				//System.out.println(words.get(i).content+" "+words.get(i).pos+" "+words.get(i).position);
			//}			
			testICTCLAS50.ICTCLAS_Exit();
		}
		catch (Exception ex)
		{
			System.out.println("parse "+ex);
		}
	}
	
	
	public void extractword()
	{
		String tagcontent = this.tag_content;
		String Words[] = tagcontent.split(" ");
		String Str = "";
		boolean iscombine = false;
		int count = 1;
		//word head = new word("","",0);
		for(int i = 0; i < Words.length; i++)
		{
			String part[] = Words[i].split("/");
			if(part.length == 2)
			{
				if(iscombine == false && (part[0].equals("\"") || part[0].equals("“") || part[0].equals("《")))
				{
					iscombine = true;
					continue;
				}
				if(iscombine && (part[0].equals("\"") || part[0].equals("”") || part[0].equals("》")))
				{
					word temp = new word(Str,"vip",count);
					count ++;
					
					iscombine = false;
					keyword.add(temp);
					viword.add(temp);
					//System.out.println(Str);
					Str = "";
					continue;
				}
				if(iscombine)
				{
					Str += part[0];
				}
				else
				{
					word temp = new word(part[0],part[1],count);
					word last;
					if(iskey(part[1],part[0]))
					{
						//if(part[1].equals("x"))
							//System.out.println(part[0]);
						
						if(words.size()>=1 && keyword.size()>=1)
						{
							word bottom = words.get(words.size()-1);
							word bottom1 = keyword.get(keyword.size()-1);
							if(bottom.content.equals(bottom1.content))
							{
								char a = bottom.pos.charAt(0);
								char b = temp.pos.charAt(0);
								if((a == b && combine))
								{
									last = new word(bottom.content+temp.content,String.valueOf(a),count-1);
									words.remove(words.size()-1);
									words.add(last);
									keyword.remove(keyword.size()-1);
									keyword.add(last);
									continue;
								}
								if((bottom.pos.equals("nr1") && temp.pos.equals("nr2")))
								{
									last = new word(bottom.content+temp.content,"nr",count-1);
									words.remove(words.size()-1);
									words.add(last);
									keyword.remove(keyword.size()-1);
									keyword.add(last);
									continue;
								}
								if(bottom.pos.equals("n") && temp.pos.equals("ng"))
								{
									last = new word(bottom.content+temp.content,"n",count-1);
									words.remove(words.size()-1);
									words.add(last);
									keyword.remove(keyword.size()-1);
									keyword.add(last);
									continue;
								}
								if(bottom.pos.startsWith("t")&&bottom.content.endsWith("月")
										&&temp.pos.startsWith("t")&&temp.content.endsWith("日"))
								{
									last = new word(bottom.content+temp.content,"t",count-1);
									//System.out.println(bottom.content+temp.content);
									words.remove(words.size()-1);
									words.add(last);
									keyword.remove(keyword.size()-1);
									keyword.add(last);
									continue;
								}

							}
						}
						//如果以“第”开头，加入viword
						//if(temp.content.startsWith("第"))
							//viword.add(temp);
						keyword.add(temp);
					}
					addword(temp);
					count ++;
				}
			}
		}
		return;
	}
	
	public boolean iskey(String pos, String str)
	{
		if(pos.startsWith("n") || pos.startsWith("t")
				||(pos.startsWith("m") && (!pos.startsWith("mq"))) ||(pos.startsWith("x") && isword(str)))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public boolean isword(String str)
	{
		for(int i = 0; i < str.length(); i++)
		{
			if((str.charAt(i)>='a'&& str.charAt(i) <='z') ||(str.charAt(i)>='A'&&str.charAt(i)<='Z'))
			{
				continue;
			}
			else
				return false;
		}
		return true;
	}
}

